package com.asiainfo.ncrm.common;


import com.asiainfo.ncrm.R;
import com.asiainfo.ncrm.base.BaseCallBack;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by 王剑洪 2016/1/21.
 */
public class TopBar extends RelativeLayout implements View.OnClickListener {
	private RelativeLayout tb;
	private LinearLayout llLeft, llRight, llCenter;
	private ImageView ivLeft, ivCenter, ivRight;
	private TextView tvLeft, tvRight, tvCenter;

	private int tbBackground;
	private String leftStr;
	private String rightStr;
	private String titleStr;
	private Drawable leftImg;
	private Drawable rightImg;
	private Drawable centerImg;
	private float besideSize;
	private float titleSize;
	private int besideColor;
	private int titleColor;
	private int type;

	private BaseCallBack left, center, right;

	public void setLeftClick(BaseCallBack listener) {
		this.left = listener;
	}

	public void setCenterClick(BaseCallBack listener) {
		this.left = listener;
	}

	public void setRightClick(BaseCallBack listener) {
		this.right = listener;
	}

	public TopBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
		type = ta.getInt(R.styleable.TopBar_type, 0);
		tbBackground = ta.getColor(R.styleable.TopBar_tb, getResources().getColor(R.color.tb_background));
		leftStr = ta.getString(R.styleable.TopBar_leftStr);
		rightStr = ta.getString(R.styleable.TopBar_rightStr);
		titleStr = ta.getString(R.styleable.TopBar_titleStr);
		leftImg = ta.getDrawable(R.styleable.TopBar_leftImg);
		rightImg = ta.getDrawable(R.styleable.TopBar_rightImg);
		centerImg = ta.getDrawable(R.styleable.TopBar_titleImg);
		besideSize = ta.getDimension(R.styleable.TopBar_besideSize, 15f);
		titleSize = ta.getDimension(R.styleable.TopBar_titleSize, 19f);
		besideColor = ta.getColor(R.styleable.TopBar_besideColor, getResources().getColor(R.color.tb_title_color));
		titleColor = ta.getColor(R.styleable.TopBar_titleColor, getResources().getColor(R.color.tb_title_color));
		// 回收TypedArray（避免浪费资源，避免因为缓存导致的错误）
		ta.recycle();

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.com_topbar, this);
		tb = (RelativeLayout) findViewById(R.id.topbar);

		tvLeft = (TextView) findViewById(R.id.top_tv_left);
		tvCenter = (TextView) findViewById(R.id.top_tv_center);
		tvRight = (TextView) findViewById(R.id.top_tv_right);

		ivLeft = (ImageView) findViewById(R.id.top_iv_left);
		ivCenter = (ImageView) findViewById(R.id.top_iv_center);
		ivRight = (ImageView) findViewById(R.id.top_iv_right);

		initAttr();
		findViewById(R.id.top_ll_left).setOnClickListener(this);
		findViewById(R.id.top_ll_center).setOnClickListener(this);
		findViewById(R.id.top_ll_right).setOnClickListener(this);
	}

	private void initAttr() {
		// 显示
		setType(type);

		tb.setBackgroundColor(tbBackground);
		tvLeft.setTextSize(besideSize);
		tvRight.setTextSize(besideSize);
		tvCenter.setTextSize(titleSize);
		tvLeft.setTextColor(besideColor);
		tvCenter.setTextColor(titleColor);
		tvRight.setTextColor(besideColor);

		tvLeft.setText("" + leftStr);
		tvCenter.setText("" + titleStr);
		tvRight.setText("" + rightStr);

		if (null != leftImg) {
			ivLeft.setImageDrawable(leftImg);
		}
		if (null != centerImg) {
			ivCenter.setImageDrawable(centerImg);
		}
		if (null != rightImg) {
			ivRight.setImageDrawable(rightImg);
		}

	}

	private void setType(int type) {
		String temp = Integer.toBinaryString(type + 64);
		View vs[] = { tvLeft, ivLeft, tvCenter, ivCenter, tvRight, ivRight };
		for (int i = 1; i < 7; i++) {
			if (Integer.parseInt(temp.substring(i, i + 1)) == 1) {
				vs[i - 1].setVisibility(View.GONE);
			}
			;
		}

	}

	public void setLeftStr(String str) {
		tvLeft.setText(str);
	}

	public void setTitleStr(String str) {
		tvCenter.setText(str);
	}

	public void setRightStr(String str) {
		tvRight.setText(str);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_ll_left:
			if (left != null) {
				left.callback();
			}
			break;
		case R.id.top_ll_center:
			if (center != null) {
				center.callback();
			}
			break;
		case R.id.top_ll_right:
			if (right != null) {
				right.callback();
			}
			break;
		}
	}
}
